<?php
/***
 * Name:	AlexSnet's framework MVC
 * About:	An MVC application framework for PHP
 * Copyright:	(C) 2008, Sudo me.
 * Author:	Alex Snet, me [at] alexsnet [dot] ru
 * Site:	http://alexsnet.ru/
 ***/

// ------------------------------------------------------------------------

/**
 * ASfMVC_View
 *
 * @package		ASfMVC
 * @author		Alex Snet
 */

class ASfMVC_View
{

 	/**
	 * $view_vars
	 *
	 * vars for view file assignment
	 *
	 * @access	public
	 */
	 var $view_vars = array();
  
 	/**
	 * class constructor
	 *
	 * @access	public
	 */
	 public function __construct() {}
  
	/**
	 * assign
	 *
	 * assign view variables
	 *
	 * @access	public
	 * @param   mixed $key key of assignment, or value to assign
	 * @param   mixed $value value of assignment
	 */    
  public function assign($key, $value=null)
  {
    if(isset($value))
      $this->view_vars[$key] = $value;
    else
      foreach($key as $k => $v)
        if(is_int($k))
          $this->view_vars[] = $v;
        else
          $this->view_vars[$k] = $v;
  }  

	/**
	 * display
	 *
	 * display a view file
	 *
	 * @access	public
	 * @param   string $filename the name of the view file
	 * @return  boolean
	 */    
  public function display($_ASMVC_filename,$view_vars=null)
  {
	$filename = ASMVC_MYAPPDIR . 'views' . DS . $_ASMVC_filename . '.php';
	if(file_exists($filename)) return $this->_view($filename,$view_vars);
	
	$filename = ASMVC_BASEDIR . 'sysfiles' . DS .  'views' . DS . $_ASMVC_filename . '.php';
	if(file_exists($filename)) return $this->_view($filename,$view_vars);
	
	return false;
  }  

	/**
	 * fetch
	 *
	 * return the contents of a view file
	 *
	 * @access	public
	 * @param   string $filename
	 * @return  string contents of view
	 */    
  public function fetch($filename,$view_vars=null)
  {
    ob_start();
    $this->display($filename,$view_vars);
    $results = ob_get_contents();
    ob_end_clean();
    return $results;
  }  

	/**
	 * sysview
	 *
	 * internal: view a system file
	 *
	 * @access	private
	 * @param   string $filename
	 * @return  boolean
	 */    
  public function sysview($filename,$view_vars = null)
  {
    $filepath = ASMVC_BASEDIR . 'sysfiles' . DS . 'views' . DS . "{$filename}.php";
    return $this->_view($filepath,$view_vars);
  }

	/**
	 * _view
	 *
	 * internal: display a view file
	 *
	 * @access	public
	 * @param   string $_ASMVC_filepath
     * @param   array $view_vars
	 */    
  	public function _view($_ASMVC_filepath,$view_vars = null)
  	{
    	if(!file_exists($_ASMVC_filepath))
      		trigger_error("Unknown file '$_ASMVC_filepath'",E_USER_ERROR);

    	// bring view vars into view scope
      	extract($this->view_vars);
    	if(isset($view_vars))
      		extract($view_vars);
    	include($_ASMVC_filepath);
	}
	/**
	 * Add some magick
	 *
	 * internal: link to assign function
	 *
	 * @access	public
	 * @param	string $key
	 * @param	string $value
	 */
	 public function __set($key,$value)
	 {
	 	$this->assign($key,$value);
	 }
	 
  

}

?>